package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;
import androidx.appcompat.widget.AppCompatEditText;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.appcompat.widget.LinearLayoutCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private AppCompatTextView contentTextView;
    private Context context  = this;
    private AppCompatEditText serach_edit;
    private AppCompatButton serach_cx;
    private AppCompatTextView contextTitle;
    private LinearLayoutCompat jc_info;
    private String city = "北海";


    private MainAdapter adapter;
    private RecyclerView recyclerViews;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        contentTextView = findViewById(R.id.contentTextView);
        serach_edit = findViewById(R.id.serach_edit);
        serach_cx = findViewById(R.id.serach_cx);
        contextTitle = findViewById(R.id.contextTitle);
        jc_info = findViewById(R.id.jc_info);
        // 获取视图
        recyclerViews = findViewById(R.id.RecyclerViews);


        OkGo.<String>get("https://tianqiapi.com/api?version=v1&appid=37278445&appsecret=7V7D5yOF").execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                WeatherInfo weatherInfo = new Gson().fromJson(response.body(),WeatherInfo.class);
                List<WeatherInfo.DataBean> dataBeans = weatherInfo.getData();
                WeatherInfo.DataBean jsonData =  dataBeans.get(0);

                contentTextView.setText("城市: " + weatherInfo.getCity() +"("+ weatherInfo.getCityEn()+ ")" +'\n');
                contentTextView.append("天气: " + jsonData.getTem2() + "~" + jsonData.getTem1()+ "," + jsonData.getWea() + '\n');
                contentTextView.append("风向: " + jsonData.getWin().get(0)+ "," + jsonData.getWin_speed() + "," + jsonData.getWin_meter()+ "\n");
                contentTextView.append("空气:" + jsonData.getAir_level() + "," + "PM2.5值为" + jsonData.getAir() + '\n');
                contentTextView.append("提示:" + jsonData.getAir_tips());
           }
        });


        serach_edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                city = serach_edit.getText().toString().trim();

                if(s.length() >= 2 ) {
                    jc_info.setVisibility(View.GONE);
                }else if(s.length() == 0){
                    jc_info.setVisibility(View.VISIBLE);
                    recyclerViews.setVisibility(View.GONE);
                }

                serach_cx.setOnClickListener(v -> {
                    if(s.length() >= 2) {
                        recyclerViews.setVisibility(View.VISIBLE);
                    }

                    OkGo.<String>get("https://tianqiapi.com/api?version=v1&appid=37278445&appsecret=7V7D5yOF&city="+ city).execute(new StringCallback() {
                        WeatherInfo.DataBean jsonData;
                        @Override
                        public void onSuccess(Response<String> response) {
                            WeatherInfo weatherInfo = new Gson().fromJson(response.body(),WeatherInfo.class);
                            List<WeatherInfo.DataBean> dataBeans = weatherInfo.getData();
                            for (int i = 0; i <dataBeans.size() ; i++) {
                                jsonData = dataBeans.get(i);
                            }

                            adapter = new MainAdapter((ArrayList<WeatherInfo.DataBean>) dataBeans);

                            recyclerViews.setLayoutManager(new LinearLayoutManager(context));
                            recyclerViews.setAdapter(adapter);

                            // 初始化事件
                            adapter.setOnItemClickListener(new MainAdapter.OnItemClickListener() {
                                @Override
                                public void onQuery(int position, WeatherInfo.DataBean info) {
                                    OkGo.<String>get("https://tianqiapi.com/api?version=v1&appid=37278445&appsecret=7V7D5yOF&city="+ city).execute(new StringCallback() {

                                        private WeatherInfo.DataBean jsonData;

                                        @Override
                                        public void onSuccess(Response<String> response) {
                                            WeatherInfo weatherInfo = new Gson().fromJson(response.body(),WeatherInfo.class);
                                            List<WeatherInfo.DataBean> dataBeans = weatherInfo.getData();
                                            for (int i = 0; i <dataBeans.size() ; i++) {
                                                jsonData = dataBeans.get(i);
                                            }
                                            dataBeans.remove(position);
                                            dataBeans.add(position,jsonData);
                                            adapter.notifyDataSetChanged();
                                        }
                                    });
                                }

                                @Override
                                public void onClick(int position, WeatherInfo.DataBean info) {
                                    Intent intent = new Intent(context,DetailActivity.class);
                                    intent.putExtra("city",city);
                                    startActivity(intent);
                                }
                            });
                        }
                    });
                });
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });



    }


}